import department from "@/api/department.js";
let pid;

const state = {
    sys_department_tree: undefined,
    department_tree: undefined,
    is_department_form_dialog_show: false,
    is_create_department: false,
    is_department_details_show: false,
    is_department_right_details_show: false,
    department_details: undefined,
    department_right_details: undefined,
    department_user_list: undefined,
    is_add_user_dialog_show: false,
    no_in_department_user_list: undefined,
    wait_del_department_user_list: undefined
}

const getters = {
    sys_department_tree: state => state.sys_department_tree,
    department_tree: state => state.department_tree,
    is_department_form_dialog_show: state => state.is_department_form_dialog_show,
    is_create_department: state => state.is_create_department,
    is_department_details_show: state => state.is_department_details_show,
    is_department_right_details_show: state => state.is_department_right_details_show,
    department_details: state => state.department_details,
    department_right_details: state => state.department_right_details,
    department_user_list: state => state.department_user_list,
    is_add_user_dialog_show: state => state.is_add_user_dialog_show,
    no_in_department_user_list: state => state.no_in_department_user_list,
    wait_del_department_user_list: state => state.wait_del_department_user_list
}

const actions = {
    async getSysDepartmentTree({
        commit
    }) {
        await department.getSysDepartmentTree().then(res => {
            commit('CHANGE_SYS_DEPARTMENT_TREE', res);
        });
    },
    async getDepartmentTree({
        commit
    }) {
        await department.getDepartmentTree().then(res => {
            commit('CHANGE_DEPARTMENT_TREE', res)
        })
    },
    async getDepartmentDetails({
        commit
    }, id) {
        await department.getDepartmentDetail(id).then(res => {
            commit('CHANGE_DEPARTMENT_DETAILS', res)
        })
    },
    async getDepartmentRightDetails({
        commit
    }, id) {
        if (!id) id = pid;
        await department.getRightDepartmentData({
            params: {
                id: id
            }
        }).then(res => {
            commit('CHANGE_DEPARTMENT_RIGHT_DETAILS', res)
        })
    },
    async getDepartmentUserList({
        commit
    }, params) {
        if (params.id) {
            pid = params.id
        } else {
            params.id = pid
        }
        params.pageNumber = params.pageNumber || 1
        await department.getDepartmentUserList({
            params: {
                departmentId: params.id,
                name: params.name,
                pageSize: 5,
                pageNumber: params.pageNumber
            }
        }).then(res => {
            commit('CHANGE_DEPARTMENT_USER_LIST', res)
        })
    },
    getNoInDepartmentUserList({
        commit
    }, data) {
        let name = data.name || ''
        let pageNumber = data.pageNumber || 1
        department.getNoInDepartmentUserList({
            params: {
                departmentId: pid,
                pageSize: 10,
                name: name,
                pageNumber: pageNumber
            }
        }).then(res => {
            commit('CHANGE_NO_IN_DEPARTMENT_USER_LIST', res)
        })
    }
}

const mutations = {
    CHANGE_SYS_DEPARTMENT_TREE(state, data) {
        state.sys_department_tree = data;
    },
    CHANGE_DEPARTMENT_TREE(state, data) {
        state.department_tree = data;
    },
    CHANGE_IS_DEPARTMENT_FORM_DIALOG_SHOW(state, data) {
        state.is_department_form_dialog_show = data;
    },
    CHANGE_IS_CREATE_DEPARTMENT(state, data) {
        state.is_create_department = data
    },
    CHANGE_IS_DEPARTMENT_DETAILS_SHOW(state, data) {
        if (data) {
            state.is_department_details_show = data
            state.is_department_right_details_show = !data
        } else {
            state.is_department_details_show = data
        }
    },
    CHANGE_IS_DEPARTMENT_RIGHT_DETAILS_SHOW(state, data) {
        if (data) {
            state.is_department_right_details_show = data
            state.is_department_details_show = !data
        } else {
            state.is_department_right_details_show = data
        }
    },
    CHANGE_DEPARTMENT_DETAILS(state, data) {
        state.department_details = data;
    },
    CHANGE_DEPARTMENT_RIGHT_DETAILS(state, data) {
        state.department_right_details = data;
    },
    CHANGE_DEPARTMENT_USER_LIST(state, data) {
        state.department_user_list = data;
    },
    CHANGE_IS_ADD_USER_DIALOG_SHOW(state, data) {
        state.is_add_user_dialog_show = data;
    },
    CHANGE_NO_IN_DEPARTMENT_USER_LIST(state, data) {
        state.no_in_department_user_list = data;
    },
    CHANGE_WAIT_DEL_DEPARTMENT_USER_LIST(state, data) {
        state.wait_del_department_user_list = data;
    }
}



export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}
